import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/assessment/assessmentGradingDto.dart';
import 'package:ichinesereader/utils/log.dart';

class EvaluationServiceProvider extends GetConnect {
  EvaluationServiceProvider() {
    httpClient.addResponseModifier((request, response) {
      if (response.statusCode == 403) {
        Log.debug('${request.url} 403 error');
        if (!Get.isSnackbarOpen) {
          Get.snackbar(
            'Message'.tr,
            'You have been logged out due to inactivity or security reasons.'
                .tr,
            snackPosition: SnackPosition.BOTTOM,
            backgroundColor: Color(0xff23c6c8),
            colorText: Color(0xffd8ffff),
          );
        }
        if (EasyLoading.isShow) {
          EasyLoading.dismiss();
        }
        if (Get.currentRoute != '/login') {
          return Get.offAllNamed('/login');
        }
      } else if (response.statusCode == 401) {
        Log.debug('${request.url} unauthorized access');
        if (!Get.isSnackbarOpen) {
          Get.snackbar('Message'.tr, 'Unauthorized access'.tr,
              backgroundColor: Color(0xff23c6c8),
              colorText: Color(0xffd8ffff),
              snackPosition: SnackPosition.BOTTOM);
        }
        if (EasyLoading.isShow) {
          EasyLoading.dismiss();
        }
        if (Get.currentRoute != '/login') {
          return Get.offAllNamed('/login');
        }
      } else {
        Log.debug(response.request?.url.toString());
        return response;
      }
    });
    httpClient.maxAuthRetries = 2;
    httpClient.timeout = Duration(seconds: 30);
  }

  Future<dynamic> getEvaluationClassReport(
      String? classId, String? level) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '${Constants.BaseAPISuper}superadmin/evaluation/report';
    if (classId != null && classId != '') url = '$url/$classId';
    if (level == null || level == '') {
      url = '$url/1';
    } else {
      url = '$url/$level';
    }
    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationBookReport(
      String? classId, String? bookcode) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url =
        '${Constants.BaseAPISuper}superadmin/evaluation/book/report/$classId/$bookcode';
    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationLinguisticKnowledges(
      String? classId, String? level, String? type) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '${Constants.BaseAPISuper}superadmin/evaluation/knowledge';
    if (classId != null && classId != '') url = '$url/$classId';
    if (level != null && level != '') url = '$url/$level';
    if (type != null && type != '') url = '$url/$type';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationProficiencySkillProgress(
      String? classId, String? level, String? type) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '${Constants.BaseAPISuper}superadmin/evaluation/skill';
    if (classId != null && classId != '') url = '$url/$classId';
    if (level != null && level != '') url = '$url/$level';
    if (type != null && type != '') url = '$url/$type';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationSkillPoints(
      String? classId, String? level) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '${Constants.BaseAPISuper}superadmin/evaluation/skillPoint';
    if (classId != null && classId != '') url = '$url/$classId';
    if (level != null && level != '') url = '$url/$level';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationProficiencySkills(
      String? classId, String? level, String? skillId) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '${Constants.BaseAPISuper}superadmin/evaluation/skill';
    if (classId != null && classId != '') url = '$url/$classId';
    if (level != null && level != '') url = '$url/$level';
    if (skillId != null && skillId != '') url = '$url/$skillId';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationOverview(String? classId) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '${Constants.BaseAPISuper}superadmin/evaluation/overview';
    if (classId != null && classId != '') url = '$url/$classId';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getBenchmarkOverview(String? classId) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url =
        '${Constants.BaseAPISuper}superadmin/examination/benchmark/overview';
    if (classId != null && classId != '') url = '$url/$classId';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationRecordsByClass(
      String? limit, String? page, String? type, String? classId) async {
    if (limit == null) limit = '0';
    if (page == null) page = '0';
    if (type == null || type == '')
      type = 'EVALUATION,BENCHMARK_BOOK,BENCHMARK_TEST';
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '';
    if (classId != null)
      url =
          '${Constants.BaseAPISuper}superadmin/question/record/list?classId=$classId&type=$type&limit=$limit&page=$page&sortField=-commitTime';
    else
      url =
          '${Constants.BaseAPISuper}superadmin/question/record/list?type=$type&limit=$limit&page=$page&sortField=-commitTime';
    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationTestClassRecords(
      String? limit, String? page, String? type, String? classId) async {
    if (limit == null) limit = '0';
    if (page == null) page = '0';
    if (type == null || type == '') type = 'BENCHMARK_BOOK,BENCHMARK_TEST';
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '';
    if (classId != null)
      url =
          '${Constants.BaseAPISuper}superadmin/question/record/list?classId=$classId&type=$type&limit=$limit&page=$page&sortField=-commitTime';
    else
      url =
          '${Constants.BaseAPISuper}superadmin/question/record/list?type=$type&limit=$limit&page=$page&sortField=-commitTime';
    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationBooks(
      String? limit, String? page, String? type, String? level) async {
    if (limit == null) limit = '0';
    if (page == null) page = '0';
    if (type == null || type == '') type = 'EVALUATION';
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '';
    if (level != null)
      url =
          '${Constants.BaseAPISuper}superadmin/evaluation/book?level=$level&type=$type&limit=$limit&page=$page';
    else
      url =
          '${Constants.BaseAPISuper}superadmin/evaluation/book?type=$type&limit=$limit&page=$page';
    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getEvaluationRandomBook(String? level) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '';
    if (level != null)
      url =
          '${Constants.BaseAPISuper}superadmin/evaluation/book/random?level=$level&type=EVALUATION';
    else
      url =
          '${Constants.BaseAPISuper}superadmin/evaluation/book/random?level=1&type=EVALUATION';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> submitEvaluationGrade(AssessmentGradingDto dto) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    final response = await put(
      '${Constants.BaseAPISuper}superadmin/examination/test',
      assessmentGradingDtoToJson(dto),
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getQuizBookRecordsByClass(
      String? limit, String? page, String? type, String? classId) async {
    if (limit == null) limit = '0';
    if (page == null) page = '0';
    if (type == null || type == '') type = 'AP,SOCIAL_STUDIES';
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url = '';
    if (classId != null)
      url =
          '${Constants.BaseAPISuper}superadmin/question/record/list?classId=$classId&type=$type&limit=$limit&page=$page&sortField=-commitTime';
    else
      url =
          '${Constants.BaseAPISuper}superadmin/question/record/list?type=$type&limit=$limit&page=$page&sortField=-commitTime';
    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }

  Future<dynamic> getBenchmarkReportUserData(
      String userName, String level) async {
    final box = Hive.box('appconfig');
    var _authtoken = box.get(CacheManagerKey['authtoken'].toString());
    String url =
        '${Constants.BaseAPISuper}superadmin/examination/benchmark/report?userName=$userName&level=$level';

    final response = await get(
      url,
      headers: <String, String>{
        'authtoken': _authtoken,
        'content-type': 'application/json;charset=UTF-8'
      },
    );
    return response;
  }
}
